package com.ruoyi.web.controller.gxs;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gxs.domain.GxsSchoolrecord;
import com.ruoyi.gxs.service.IGxsSchoolrecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学习成绩Controller
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/gxs/schoolrecord")
public class GxsSchoolrecordController extends BaseController
{
    @Autowired
    private IGxsSchoolrecordService gxsSchoolrecordService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询学习成绩列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:schoolrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxsSchoolrecord gxsSchoolrecord)
    {
        startPage();
        List<GxsSchoolrecord> list = gxsSchoolrecordService.selectGxsSchoolrecordList(gxsSchoolrecord);
        return getDataTable(list);
    }

    /**
     * 导出学习成绩列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:schoolrecord:export')")
    @Log(title = "学习成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsSchoolrecord gxsSchoolrecord)
    {
        List<GxsSchoolrecord> list = gxsSchoolrecordService.selectGxsSchoolrecordList(gxsSchoolrecord);
        ExcelUtil<GxsSchoolrecord> util = new ExcelUtil<GxsSchoolrecord>(GxsSchoolrecord.class);
        util.exportExcel(response, list, "学习成绩数据");
    }

    /**
     * 获取学习成绩详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:schoolrecord:query')")
    @GetMapping(value = "/{gxsId}")
    public AjaxResult getInfo(@PathVariable("gxsId") Long gxsId)
    {
        return success(gxsSchoolrecordService.selectGxsSchoolrecordByGxsId(gxsId));
    }

    /**
     * 新增学习成绩
     */
//    @PreAuthorize("@ss.hasPermi('gxs:schoolrecord:add')")
    @Log(title = "学习成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsSchoolrecord gxsSchoolrecord)
    {
        //添加用id、学号、部门id
        SysUser sysUser = sysUserService.selectUserById(getUserId());
        gxsSchoolrecord.setUserId(sysUser.getUserId());
        gxsSchoolrecord.setUserName(sysUser.getUserName());
        gxsSchoolrecord.setDeptId(sysUser.getDeptId());

        //创建者和创建时间
        gxsSchoolrecord.setCreateBy(getUsername());
        gxsSchoolrecord.setCreateTime(new Date());
        return toAjax(gxsSchoolrecordService.insertGxsSchoolrecord(gxsSchoolrecord));
    }

    /**
     * 修改学习成绩
     */
//    @PreAuthorize("@ss.hasPermi('gxs:schoolrecord:edit')")
    @Log(title = "学习成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsSchoolrecord gxsSchoolrecord)
    {
        return toAjax(gxsSchoolrecordService.updateGxsSchoolrecord(gxsSchoolrecord));
    }

    /**
     * 删除学习成绩
     */
//    @PreAuthorize("@ss.hasPermi('gxs:schoolrecord:remove')")
    @Log(title = "学习成绩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsIds}")
    public AjaxResult remove(@PathVariable Long[] gxsIds)
    {
        return toAjax(gxsSchoolrecordService.deleteGxsSchoolrecordByGxsIds(gxsIds));
    }


//    //用户导入
//    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
//    @PostMapping("/importData")
//    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
//    {
//        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
//        List<SysUser> userList = util.importExcel(file.getInputStream());
//        String operName = getUsername();
//        String message = userService.importUser(userList, updateSupport, operName);
//        return success(message);
//    }
//
//    //用户导入模板
//    @PostMapping("/importTemplate")
//    public void importTemplate(HttpServletResponse response)
//    {
//        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
//        util.importTemplateExcel(response, "用户数据");
//    }


    /**
     * 我的学习成绩列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:schoolrecord:list')")
    @GetMapping("/MyList")
    public TableDataInfo MyList(GxsSchoolrecord gxsSchoolrecord)
    {
        startPage();
        List<GxsSchoolrecord> list = gxsSchoolrecordService.selectGxsSchoolrecordList(gxsSchoolrecord);

        String username = getUsername();

        for (int i = 0 ; i < list.size(); i++){
            String userName = list.get(i).getUserName();

            if (!userName.equals(username)) {
                list.remove(i);
                i--;
            }
        }

        return getDataTable(list);
    }
}
